<?php

namespace app\astro\controller;

use \app\check\controller\Check;

class Index extends Check
{
    /**
     * 获取档案星盘数据
     * @param null $year
     * @param null $month
     * @param null $day
     * @param null $hour
     * @param null $minute
     * @param null $longitude
     * @param null $latitude
     * @param int $timezone
     * @param string $h_sys
     * @return array
     */
    public function getTheRecodeAstro($year = null, $month = null, $day = null, $hour = null, $minute = null,
                                      $longitude = null, $latitude = null, $timezone = 8, $h_sys = 'p')
    {
        $time = $this->getTheBaseTime($year, $month, $day, $hour, $minute, $timezone);
        $utdatenow = strftime('%d.%m.%Y', $time);
        $utnow = strftime('%H:%M:%S', $time);

        exec(config('astrolog.prefix') . " -b{$utdatenow} -ut{$utnow} -p0123456789DAmHt -eswe -house{$longitude},{$latitude},{$h_sys} -flds -g, -head", $out);

        if (empty($out) || count($out) < 27)
        {
            unset($out);
            exec(config('astrolog.prefix') . " -b{$utdatenow} -ut{$utnow} -p0123456789DAmHt -eswe -house{$longitude},{$latitude},w -flds -g, -head", $out);
        }

        $data = $this->getOriginalData($out);

        $result = [];
        $result['sun'] = InWhichSign($data['p1'][0]) . '-' . InWhichHouse($data['p1'][0], $data['hc1']) . '-' . $data['p1'][0];
        $result['moon'] = InWhichSign($data['p1'][1]) . '-' . InWhichHouse($data['p1'][1], $data['hc1']) . '-' . $data['p1'][1];
        $result['merc'] = InWhichSign($data['p1'][2]) . '-' . InWhichHouse($data['p1'][2], $data['hc1']) . '-' . $data['p1'][2];
        $result['venu'] = InWhichSign($data['p1'][3]) . '-' . InWhichHouse($data['p1'][3], $data['hc1']) . '-' . $data['p1'][3];
        $result['mars'] = InWhichSign($data['p1'][4]) . '-' . InWhichHouse($data['p1'][4], $data['hc1']) . '-' . $data['p1'][4];
        $result['jupi'] = InWhichSign($data['p1'][5]) . '-' . InWhichHouse($data['p1'][5], $data['hc1']) . '-' . $data['p1'][5];
        $result['satu'] = InWhichSign($data['p1'][6]) . '-' . InWhichHouse($data['p1'][6], $data['hc1']) . '-' . $data['p1'][6];
        $result['non'] = InWhichSign($data['p1'][12]) . '-' . InWhichHouse($data['p1'][12], $data['hc1']) . '-' . $data['p1'][12];
        $result['lili'] = InWhichSign($data['p1'][11]) . '-' . InWhichHouse($data['p1'][11], $data['hc1']) . '-' . $data['p1'][11];
        $result['juno'] = InWhichSign($data['p1'][13]) . '-' . InWhichHouse($data['p1'][13], $data['hc1']) . '-' . $data['p1'][13];
        $result['asc'] = InWhichSign($data['p1'][17]) . '-' . InWhichHouse($data['p1'][17], $data['hc1']) . '-' . $data['p1'][17];
        $result['mc'] = InWhichSign($data['p1'][20]) . '-' . InWhichHouse($data['p1'][20], $data['hc1']) . '-' . $data['p1'][20];

        return $result;
    }

    /**
     * 初始化本命盘
     * @param array $data
     * @param string $h_sys
     * @return array
     */
    public function getOriginalData($data = [], $h_sys = 'p')
    {
        $planet = [];
        $speed = [];
        foreach ($data as $key => $line)
        {
            $row = explode(',', $line);
            $planet[$key] = @floatval($row[0]) * 1;
            $declination[$key] = isset($row[1]) ? floatval($row[1]) * 1 : 0;
            $speed[$key] = isset($row[2]) ? floatval($row[2]) * 1 : 0;
        };

        // 判断是日间盘还是夜间盘
        if ($planet[15] > $planet[21])
            if ($planet[0] <= $planet[15] And $planet[0] > $planet[21])
                $day_chart = true;
            else
                $day_chart = false;
        else
            if ($planet[0] > $planet[15] And $planet[0] <= $planet[21])
                $day_chart = false;
            else
                $day_chart = true;

        $juno = $planet[13];// 婚神星
        // 计算福点位置
        if ($day_chart == true) $fort = $planet[15] + $planet[1] - $planet[0];
        else $fort = $planet[15] - $planet[1] + $planet[0];
        if ($fort >= 360) $fort -= 360;
        if ($fort < 0) $fort += 360;

        // 计算宿命点度数
        $vert = $planet[30];

        $asc = $planet[27];
        $mc = $planet[28];
        $des = $asc >= 180 ? $asc - 180 : $asc + 180;
        $ic = $mc >= 180 ? $mc - 180 : $mc + 180;

        $hc = [];

        for ($i = 1; $i <= 14; $i++)
        {
            $hc[$i] = $planet[14 + $i];
            unset($planet[14 + $i]);
        }

        unset($hc[13]);
        unset($hc[14]);
        unset($planet[29]);
        unset($planet[30]);

        $planet[13] = $juno;// 婚神星
        $planet[14] = $fort;// 福点
        $planet[15] = $vert;// 宿命点
        $planet[16] = $planet[12] > 180 ? $planet[12] - 180 : $planet[12] + 180;// 增加南交点
        // 加四轴 放入行星组中
        $planet[17] = $asc;// 上升点
        $planet[18] = $ic;// 天底
        $planet[19] = $des;// 下降点
        $planet[20] = $mc;// 天顶

        $retro = $this->getRetroData($speed);

        $returnData = ['rx1' => $retro, 'p1' => $planet, 'hc1' => $hc, 'speed' => $speed];

        return $returnData;
    }

    /**
     * 获取是否逆行数据
     * @param  [type] $speed [description]
     * @return string [type]        [description]
     */
    protected function getRetroData($speed)
    {
        $retro = '';

        for ($i = 0; $i <= 13; $i++)
        {
            if (@$speed[$i] < 0) $retro .= 'R';
            else $retro .= ' ';
        }

        return $retro;
    }

    /**
     * 获取基础时间
     * @param null $year
     * @param null $month
     * @param null $day
     * @param null $hour
     * @param null $minute
     * @param int $timezone
     * @return false|int
     */
    private function getTheBaseTime($year = null, $month = null, $day = null, $hour = null, $minute = null, $timezone = 8)
    {
        if (isSummerTime($hour, $minute, 0, $month, $day, $year))
        {
            $hour--;// 夏令时减一小时
        }

        if ($timezone >= 0)
        {
            $whole = floor($timezone);
            $fraction = $timezone - floor($timezone);
        } else
        {
            $whole = ceil($timezone);
            $fraction = $timezone - ceil($timezone);
        }
        $hour = $hour - $whole;
        $minute = $minute - ($fraction * 60);

        return mktime($hour, $minute, 0, $month, $day, $year);
    }
}
